package objetos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;




public class GamePanel extends JPanel implements Runnable {
	public static final int PWIDTH = 640;
	public static final int PHEIGHT = 480;
	


	public static GamePanel instance;
	
	URL url = this.getClass().getResource("imagem.png");  
	static Image imagemTitulo; 

	private Thread animator;
	public boolean running = false;
	int FPS, SFPS;
	int fpscount;

	
	public int faseAtual2 = 1;
	
	public int faseMaxLiberada = 0;
	
	
	public static myCanvas canvasAtivo = null;



	private BufferedImage dbImage;
	private Graphics2D dbg;



	public GamePanel() {
		instance = this;

		setBackground(Color.white);
		
		
		

		
		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
		
		imagemTitulo = loadImage("./../icone.png");
		
		//this.setIconImage(Toolkit.getDefaultToolkit().getImage(url));  
		
		    

		// create game components
		setFocusable(true);

		requestFocus(); // JPanel now receives key events

		if (dbImage == null) {
			dbImage = new BufferedImage(PWIDTH, PHEIGHT,
					BufferedImage.TYPE_INT_ARGB);
			if (dbImage == null) {
				System.out.println("dbImage is null");
				return;
			} else {
				dbg = (Graphics2D) dbImage.getGraphics();
			}
		}




		// Adiciona um Key Listner
		addKeyListener( new KeyAdapter() {
			public void keyPressed(KeyEvent e)
			{ 
				if(canvasAtivo!=null){
					canvasAtivo.keyPressed(e);
				}
			}
			@Override
			public void keyReleased(KeyEvent e ) {
				if(canvasAtivo!=null){
					canvasAtivo.keyReleased(e);
				}
			}
		});

		addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				if(canvasAtivo!=null){
					canvasAtivo.mouseMoved(e);
				}
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				if(canvasAtivo!=null){
					canvasAtivo.mouseDragged(e);
				}
			}


		});


		canvasAtivo = new CanvasGame();



	} // end of GamePanel()
	
	
	

	public void addNotify() {
		super.addNotify(); // creates the peer
		startGame(); // start the thread
	}

	private void startGame()
	// initialise and start the thread
	{
		if (animator == null || !running) {
			animator = new Thread(this);
			animator.start();
		}
	} // end of startGame()

	public void stopGame()
	// called by the user to stop execution
	{
		running = false;
	}

	public void run()
	/* Repeatedly update, render, sleep */
	{
		running = true;

		long DifTime, TempoAnterior;
		long relogioTeste = 0;
		int segundo = 0;
		DifTime = 0;
		TempoAnterior = System.currentTimeMillis();

		while (running) {

			DifTime = System.currentTimeMillis() - TempoAnterior;
			TempoAnterior = System.currentTimeMillis();
			
			gameUpdate(DifTime); // game state is updated
			gameRender(); // render to a buffer
			paintImmediately(0, 0, PWIDTH, PHEIGHT); // 640  480 paint with the buffer
			relogioTeste += DifTime;
			
			//System.out.println("GamePanel::run : "+relogioTeste/1000);
			try {
				Thread.sleep(0); // sleep a bit
			} catch (InterruptedException ex) {
			}


			if (segundo != ((int) (TempoAnterior / 1000))) {
				FPS = SFPS;
				SFPS = 1;
				segundo = ((int) (TempoAnterior / 1000));
			} else {
				SFPS++;
			}

		}
		System.exit(0); // so enclosing JFrame/JApplet exits
	} // end of run()

	int timerfps = 0;

	private void gameUpdate(long DiffTime)
	{ 
		if(canvasAtivo!=null){
			canvasAtivo.SimulaSe(DiffTime);
		}
	}

	private void gameRender()
	// draw the current frame to an image buffer
	{
		if(canvasAtivo!=null){
			canvasAtivo.DesenhaSe(dbg);
		}		
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (dbImage != null)
			g.drawImage(dbImage, 0, 0, null);
	}

	public static void main(String args[]) {
		GamePanel ttPanel = new GamePanel();
		ttPanel.setSize(1000, 1000);
		// create a JFrame to hold the timer test JPanel
		JFrame app = new JFrame("SHADOW DREAM");
		app.setIconImage(imagemTitulo);  
		
		app.getContentPane().add(ttPanel, BorderLayout.CENTER);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setResizable(true);
		app.setFocusableWindowState(true);
		app.setSize(800, 800);
		app.setVisible(true);
		app.pack();
		
		

	} // end of main()

	public static BufferedImage loadImage(String source) {
		BufferedImage image = null;
		try {
			BufferedImage tmp = ImageIO.read(GamePanel.instance.getClass()
					.getResource(source));
			image = new BufferedImage(tmp.getWidth(), tmp.getHeight(),
					BufferedImage.TYPE_INT_ARGB);
			image.getGraphics().drawImage(tmp, 0, 0, null);
			tmp = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;
	}

} // end of GamePanel class

